/*******************************************************************************
 * Copyright (c) 2009 Kevin Bierhoff and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Kevin Bierhoff - initial API and implementation
 *******************************************************************************/
package com.googlecode.twelfdevtools.views;

import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;

import com.googlecode.twelfdevtools.model.IElfFile;
import com.googlecode.twelfdevtools.model.ITwelfConstant;
import com.googlecode.twelfdevtools.model.ITwelfModelElement;
import com.googlecode.twelfdevtools.model.ITwelfNamedElement;


public class ElfFileContentProvider implements ITreeContentProvider {

	private static final Object[] EMPTY_ARRAY = new Object[0];

	public void dispose() {
	}

	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
	}

	public Object[] getChildren(Object parentElement) {
		if(parentElement instanceof IElfFile) {
			IElfFile f = (IElfFile) parentElement;
			return f.getChildren().toArray(EMPTY_ARRAY);
		}
		if(parentElement instanceof ITwelfConstant) {
			ITwelfConstant f = (ITwelfConstant) parentElement;
			return f.getChildren().toArray(EMPTY_ARRAY);
		}
		return EMPTY_ARRAY;
	}

	public Object getParent(Object element) {
		if(element instanceof IElfFile)
			// stop at file
			return null;
		if(element instanceof ITwelfNamedElement) {
			ITwelfModelElement e = (ITwelfModelElement) element;
			return e.getParent();
		}
		// Elf file not a named element -> parent is null
		return null;
	}

	public boolean hasChildren(Object element) {
		return getChildren(element).length > 0;
	}

	public Object[] getElements(Object inputElement) {
		return getChildren(inputElement);
	}

}
